from memcache import memcache
import memcacheq

from textwrap import TextWrapper

class MemqReader(object):

    def __init__(self, memcacheq_queue):
        self.mq = memcacheq_queue
        
        self.running = True
        
        print 'memqReader init'
    
    def run(self):
        while self.running:
            
            if len(self.mq) != 0:
                status = self.mq.get()
                
                try:
                    print TextWrapper(width=60, initial_indent='    ', subsequent_indent='    ').fill(status.text)
                
                    print '\n %s  %s  via %s\n' % (status.author.screen_name, status.created_at, status.source)
                    
                except:
                    pass
            
def main():
    mqr = MemqReader(memcacheq.MemcacheQueue('app_tweets', memcache.Client(['127.0.0.1:11211'])))
    mqr.run()


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print '\nGoodbye!'